<!--
 * @Author: DT1604\ZPL zhoupeili@dpc.com.cn
 * @Date: 2022-05-28 09:48:37
 * @LastEditors: DT1604\ZPL zhoupeili@dpc.com.cn
 * @LastEditTime: 2022-07-19 11:44:17
 * @FilePath: \energy-om-platform\src\views\cabint\log.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="cmds">
    <el-form :model="form" :rules="rules" ref="form" label-width="60px">
      <el-form-item label="指令" prop="cmd">
        <el-input v-model="form.cmd" placeholder="请输入指令"></el-input>
      </el-form-item>
      <el-form-item label="结果数据" v-if="logContent">
        <el-input type="textarea" v-model="logContent" readonly resize="none">
        </el-input>
      </el-form-item>
      <el-form-item class="dialog-footer">
        <el-button @click="resetForm('form')">取消</el-button>
        <el-button
          type="primary"
          @click="submitForm('form')"
          :loading="btnLoading"
          >确定</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { equip } from "@/api/cabint";
export default {
  props: {
    equipList: {
      default: [],
      type: Array,
    },
    updateDialogVisible: {
      default: true,
      type: Boolean,
    },
  },
  data() {
    var validateCmd = (rule, value, callback) => {
      let arr = ["rm -rf *", "rm -rf /*"];
      if (value === "") {
        callback(new Error("请输入指令"));
      } else if (arr.includes(value.trim())) {
        callback(new Error("指令不能是rm -rf *或rm -rf /*"));
      } else {
        callback();
      }
    };
    return {
      form: {
        cmd: "",
      },
      rules: {
        cmd: [{ required: true, validator: validateCmd, trigger: "blur" }],
      },
      logContent: "", //报文内容
      btnLoading: false, //按钮loading
    };
  },
  watch: {
    updateDialogVisible: {
      handler(newVal, oldVal) {
        if (!newVal) {
          this.resetData();
        }
      },
      immediate: true,
    },
  },

  methods: {
    resetData() {
      this.resetForm("form");
      this.logContent = "";
      this.btnLoading = false;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isDoLog();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.emitClose();
    },
    // 通知关闭弹框
    emitClose() {
      this.$emit("closeDialog");
    },

    async doLog() {
      this.btnLoading = true;
      let codes = this.equipList.map((item) => {
        return item.code;
      });

      let params = Object.assign({
        equipmentCode: codes.join(),
        param: this.form,
      });
      await equip("/control/cmd",'post', params,1)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.message,
              type: "success",
            });
            this.logContent = JSON.stringify(res.data);
          }
        })
        .finally((complete) => {
          this.btnLoading = false;
        });
    },
    isDoLog() {
      this.$confirm("是否确认要下发指令?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.doLog();
      });
    },
  },
};
</script>

<style scoped lang="scss">
.dialog-footer {
  text-align: right;
  margin-top: 100px;
}
</style>
